package com.gpframework.common.utils.tencent;

import com.alibaba.fastjson.JSONObject;
import com.gpframework.common.constant.HttpRequestPathConstant;
import com.gpframework.common.constant.HttpStatus;
import com.gpframework.common.utils.http.HttpUtils;
import com.gpframework.common.utils.tencent.domian.TencentMapData;
import com.gpframework.common.utils.tencent.domian.TencentMapResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * 腾讯位置服务，经纬度转实现类
 */
@Component
public class LocationSearchUtils {

    protected static Logger logger = LoggerFactory.getLogger(LocationSearchUtils.class);

    /**
     * 地址获取经纬度所使用的KEY
     */
    public static String GRIDKEY;

    /**
     * 两个地址距离运算所使用的KEY
     */
    public static String DISKEY;

    @Value("${gpframework.gridKey}")
    public String gridKey;

    @Value("${gpframework.disKey}")
    public String disKey;

    /**
     * 静态注入
     */
    @PostConstruct
    public void init() {
        LocationSearchUtils.GRIDKEY = this.gridKey;
        LocationSearchUtils.DISKEY = this.disKey;
    }

    public static void main(String[] args) {
        String adress = "北京市海淀区彩和坊路海淀西大街74号";
        TencentMapData tencentMapData = LocationSearchUtils.wordToGrid("4XYBZ-EJBW3-UR234-3AZ4L-QO3MF-2MFO3", adress);
        logger.info(String.valueOf(tencentMapData));

    }

    /**
     * 文字转坐标
     *
     * @param address 地址
     * @return
     */
    public static TencentMapData wordToGrid(String address) {
        String url = String.format(HttpRequestPathConstant.TENCENT_LOCATION_SEARCH, DISKEY, address);
        String returnStr = HttpUtils.sendGet(url);
        TencentMapResult tencentMapResult = JSONObject.parseObject(returnStr, TencentMapResult.class);
        if (HttpStatus.TENCENT_MAP_SUCCESS != tencentMapResult.getStatus()) {
            logger.info("----------获取经纬度失败" + tencentMapResult + "----------");
            return null;
        }
        return tencentMapResult.getResult();
    }

    /**
     * 文字转坐标
     *
     * @param key     腾讯地图key
     * @param address 地址
     * @return
     */
    public static TencentMapData wordToGrid(String key, String address) {
        String url = String.format(HttpRequestPathConstant.TENCENT_LOCATION_SEARCH, key, address);
        String returnStr = HttpUtils.sendGet(url);
        TencentMapResult tencentMapResult = JSONObject.parseObject(returnStr, TencentMapResult.class);
        if (HttpStatus.TENCENT_MAP_SUCCESS != tencentMapResult.getStatus()) {
            logger.info("----------获取经纬度失败" + tencentMapResult + "----------");
            return null;
        }
        return tencentMapResult.getResult();
    }

    /**
     * 地址解析：两地距离运算
     *
     * @param disFrom 来自的地址
     * @param disTo   去的地址
     * @return
     * @throws IOException
     */
/*    public static Map<String, String> distanceCalcula(String[] disFrom, String[] disTo) throws IOException {
        String urls = "http://apis.map.qq.com/ws/distance/v1/?mode=driving&from=" + disFrom[0] + "," + disFrom[1] + "&to=" + disTo[0] + "," + disTo[1] + "&key=" + DISKEY;
        Map<String, String> map = Maps.newHashMap();
        // 1.地理编码服务，即根据地址来查经度纬度
        String return_value = do_get(urls);
        logger.info(return_value);
        // {"status":0,"result":{"location":{"lng":121.51996737545,"lat":31.308233584217},"precise":1,"confidence":80,"level":"\u5546\u52a1\u5927\u53a6"}} Map<String, String> map = Maps.newHashMap();
        net.sf.json.JSONObject joR = net.sf.json.JSONObject.fromObject(return_value);
        // 正常返回
        if ("0".equals(joR.get("status").toString())) {
            if (joR.getJSONObject("result").has("elements")) {
                JSONObject infoJosn = (JSONObject) joR.getJSONObject("result").getJSONArray("elements").get(0);
                map.put("status", joR.get("status").toString());
                map.put("distance", infoJosn.get("distance").toString());
            }
        }
        // 异常
        else {
            logger.info("错误提示：" + joR.get("status"));
            map.put("status", joR.get("status").toString());
        }
        return map;
    }*/

    /*
        public static void main(String[] args) throws IOException {

            String key = "4XYBZ-EJBW3-UR234-3AZ4L-QO3MF-2MFO3";
            //String url = "http://apis.map.qq.com/ws/geocoder/v1/?address=上海五角场万达广场&output=json&key=" + key;
            //String urls="http://restapi.amap.com/v3/geocode/geo?key=e45d1d4006fe1a9f9f1bd653976d27f1&address=方恒国际中心A座&city=北京";
            String urls="http://apis.map.qq.com/ws/distance/v1/?mode=driving&from=39.983171,116.308479&to=39.996060,116.353455&key=4XYBZ-EJBW3-UR234-3AZ4L-QO3MF-2MFO3";
            Map<String, String> map = Maps.newHashMap();
            // 1.地理编码服务，即根据地址来查经度纬度
            String return_value = do_get(urls);
            logger.info(return_value);
            // {"status":0,"result":{"location":{"lng":121.51996737545,"lat":31.308233584217},"precise":1,"confidence":80,"level":"\u5546\u52a1\u5927\u53a6"}} Map<String, String> map = Maps.newHashMap();
            net.sf.json.JSONObject joR = net.sf.json.JSONObject.fromObject(return_value);
            // 正常返回
            if ("0".equals(joR.get("status").toString())) {
                // Ing经度
                String Ing = joR.getJSONObject("result").getJSONObject("location").get("lng").toString();
                // Lat纬度
                String Lat = joR.getJSONObject("result").getJSONObject("location").get("lat").toString();
                map.put("status", joR.get("status").toString());
                map.put("lay", Lat);
                map.put("Ing", Ing);
            }
            // 异常
            else {
                logger.info("错误提示：" + joR.get("status"));
                map.put("status", joR.get("status").toString());
            }

        }*/

	/*public static void main(String[] args) throws IOException {
		String urls="http://apis.map.qq.com/ws/distance/v1/?mode=driving&from=23.075332,113.275672&to=23.126784,113.316459&key=4XYBZ-EJBW3-UR234-3AZ4L-QO3MF-2MFO3";
		Map<String, String> map = Maps.newHashMap();
		// 1.地理编码服务，即根据地址来查经度纬度
		String return_value = do_get(urls);
		logger.info(return_value);
		// {"status":0,"result":{"location":{"lng":121.51996737545,"lat":31.308233584217},"precise":1,"confidence":80,"level":"\u5546\u52a1\u5927\u53a6"}} Map<String, String> map = Maps.newHashMap();
		net.sf.json.JSONObject joR = net.sf.json.JSONObject.fromObject(return_value);
		// 正常返回
		if ("0".equals(joR.get("status").toString())) {
			if(joR.getJSONObject("result").has("elements")){
				JSONObject goodInfoJosn=(JSONObject) joR.getJSONObject("result").getJSONArray("elements").get(0);
				logger.info(goodInfoJosn.get("distance"));
			}
		}
		// 异常
		else {
			logger.info("错误提示：" + joR.get("status"));
			map.put("status", joR.get("status").toString());
		}

	}*/
}
